package com.gwsdk.paysdk.bean;

import com.gwsdk.open.FlagControl;
import com.gwsdk.open.GWApi;
import com.gwsdk.open.LogoutCallback;
import com.gwsdk.paysdk.dialog.DialogUtil;
import com.gwsdk.paysdk.entity.ChannelAndGameinfo;
import com.gwsdk.paysdk.manager.GWGameManager;
import com.gwsdk.paysdk.utils.AppStatusManager;
import com.gwsdk.paysdk.utils.ToastUtil;
import com.gwsdk.paysdk.utils.SharedPreferencesUtils;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;
import android.view.View;

public class PersonalCenterModel {

    public ChannelAndGameinfo channelAndGame = null;

    private boolean isUpdatePwd;

    private static PersonalCenterModel instance;

    public static PersonalCenterModel getInstance() {
        if (null == instance) {
            instance = new PersonalCenterModel();
        }
        return instance;
    }

    public PersonalCenterModel() {
        channelAndGame = new ChannelAndGameinfo();
        isUpdatePwd = false;
    }

    public void clearUserInfoAll(){
        channelAndGame.setClearAll();
    }

    public String getAccount() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getAccount();
    }
    public String getEmail() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.geteMail();
    }

    public String getIs_uc() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getIs_uc();
    }

    public String getReal_name() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getReal_name();
    }

    public String getIdcard() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getIdcard();
    }

    public String getGameName() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getGameName();
    }

    public String getPhone() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getPhoneNumber();
    }

    public String getUserPtb() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getPlatformMoney() + "";
    }

    public String getBindPtb() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getBindPtbMoney() + "";
    }

    public void setPhone(String phone) {
        if (null != channelAndGame && !TextUtils.isEmpty(phone)) {
            channelAndGame.setPhoneNumber(phone);
        }
    }

    public boolean isUpdatePwd() {
        return isUpdatePwd;
    }

    public void setUpdatePwd(boolean isUpdatePwd) {
        this.isUpdatePwd = isUpdatePwd;
    }

    public String getUserId() {
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getUserId();
    }

    public String getToken(){
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getToken();
    }

    public String getSmallAccountToken(){
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getSmallAccountToken();
    }

    public String getSmallAccountUserID(){
        if (null == channelAndGame) {
            return "";
        }
        return channelAndGame.getSmallAccountUserId();
    }

    public void clearUserLoginInfo(final Activity context, final LogoutCallback logoutCallback) {
        if (!LoginModel.instance().isLogin()) {
            ToastUtil.show(context, "用户未登录");
            return;
        }

        GWGameManager.getInstance().userLogin = null;
        GWGameManager.getInstance().loginSmallAccount = null;
        AppStatusManager.getInstance().userLogin = null;
        InitModel.init().offLine(context, true);
        channelAndGame = new ChannelAndGameinfo();
        if (logoutCallback != null) {
            try {
                SharedPreferencesUtils.getInstance().setIsLogout(context,true);     //注销状态设为trueThread.sleep(500);
                PersonalCenterModel.getInstance().clearUserInfoAll();
                Thread.sleep(100);

                logoutCallback.logoutResult("1");
                FlagControl.flag = true;
                FlagControl.isLogin = false;
                FlagControl.isFloatingOpen = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void clearUserLoginInfoShowDialog(final Activity activity, final LogoutCallback logoutCallback) {
        Activity context = activity;
        if (context == null) {
            context = GWApi.getApi().getContext();
        }
        if (!LoginModel.instance().isLogin()) {
            ToastUtil.show(context, "用户未登录");
            return;
        }

        final Activity finalContext = context;
        Dialog dialog = DialogUtil.gw_alert_msg(context, "提示", "您确定要退出当前账号吗？", context, "确定", "取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InitModel.init().offLine(finalContext, true);
                channelAndGame = new ChannelAndGameinfo();
                GWGameManager.getInstance().userLogin = null;
                GWGameManager.getInstance().loginSmallAccount = null;
                AppStatusManager.getInstance().userLogin = null;
                if (logoutCallback != null) {
                    try {
                        SharedPreferencesUtils.getInstance().setIsLogout(finalContext,true);     //注销状态设为trueThread.sleep(500);
                        PersonalCenterModel.getInstance().clearUserInfoAll();
                        Thread.sleep(100);
                        if (activity != null) {
                            activity.finish();
                        }

                        logoutCallback.logoutResult("1");
                        FlagControl.flag = true;
                        FlagControl.isLogin = false;
                        FlagControl.isFloatingOpen = false;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        dialog.show();
    }
}
